package com.longchat.controller.admin;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.longchat.domain.permission.SysUser;
import com.longchat.utils.Constant;
import com.longchat.utils.Rc4Utils;

public class BaseController {
	private static final Logger logger = LoggerFactory.getLogger(BaseController.class);
	protected static final String ENCRY_LAST_STR = "~!@#$%^&*(QAZWSXPLM)!@#~";
	public SysUser getUser(HttpSession session){
		return (SysUser)session.getAttribute(Constant.SESSION_USER);
	}
	protected String getDomain(HttpServletRequest httpRequest){
		String requri = httpRequest.getRequestURI();
		String requrl = httpRequest.getRequestURL().toString();
		String domain =requrl.substring(0, (requrl.length()-requri.length()));
		return domain; //参数  
	}
	protected String getOpenid(HttpServletRequest request){
		Cookie[] cookies = request.getCookies();
		String openid="";
		if(cookies!=null){
			for(Cookie cookie:cookies){
				if(cookie.getName().equals("openid")){
					 openid = Rc4Utils.encry_RC4_string(cookie.getValue(),Rc4Utils.KEY);
					 if(openid.indexOf(ENCRY_LAST_STR)==-1){
						 logger.error("openid错误，可能该用户伪装openid抽奖,openid={},encryOpenid={}",openid);
						 throw new RuntimeException("openid错误，可能该用户伪装openid抽奖");
					 }else{
						 openid = openid.replace(ENCRY_LAST_STR,"");
					 }
				}
			}
		}
		return openid;
	}
	protected String getNickName(HttpServletRequest request){
		Cookie[] cookies = request.getCookies();
		String nickName="";
		if(cookies!=null){
			for(Cookie cookie:cookies){
				if(cookie.getName().equals("nickName")){
					return nickName;
				}
			}
		}
		return nickName;
	}
	
}
